window.$ = WLJQ;

var Authenticator = function () {
    var LOGIN_PAGE_SECURITY_INDICATOR = 'j_security_check';
    var USERNAME_INPUT_ID = '#usernameInputField';
    var PASSWORD_INPUT_ID = '#passwordInputField';
    var LOGIN_BUTTON_ID   = '#loginButton';  
    var onSubmitCallback  = null;

    function onFormSubmit() {
        var reqURL = './' + LOGIN_PAGE_SECURITY_INDICATOR;
        var params = {
            j_username : $(USERNAME_INPUT_ID).val(),
            j_password : $(PASSWORD_INPUT_ID).val()
        };

        onSubmitCallback(reqURL, {parameters:params});
    }

    return {
        init : function () {
            $(LOGIN_BUTTON_ID).bind('click', onFormSubmit);
        }, 

        isLoginFormResponse : function (response) {            
            if (!response || response.responseText === null) {
                return false;
            }
            if(response.status == 403) 
            	alert("用户名密码有错");
            var indicatorIdx = response.responseText.search(LOGIN_PAGE_SECURITY_INDICATOR);
            WL.Logger.debug("登陆结果"+response);
            return (indicatorIdx >= 0);
        },
                
        onBeforeLogin : function (response, username, onSubmit, onCancel) {
            onSubmitCallback = onSubmit;
            onCancelCallback = onCancel;            
            if (!Object.isUndefined(username) && username != null){
                $(USERNAME_INPUT_ID).val(username);
            }
            else {
                $(USERNAME_INPUT_ID).val('');
            }
            $(PASSWORD_INPUT_ID).val('');
        },
        
    	onShowLogin: function() {
    		$('#AppBody').hide();
    		$('#AuthBody').show();
    	},
        
    	onHideLogin: function(){    		
    		$('#AppBody').show();
    		$('#AuthBody').hide();
        }   
    }; 
}();